<template>
    <div class="pageContainer">
        <div class="searchWarp">
            <el-form inline>
                <template v-for="condition in listCondition">
                    <CText :title="condition.name" v-model="condition.value"></CText>
                </template>
            </el-form>
            <div class="schLine">
                <CBtn @click="search(1)">查询</CBtn>
            </div>
        </div>
        <div class="treeTable">
            <div style="flex:1;">
                <div class="btnWarp">
                    <CBtn @click="showEdit">新增</CBtn>
                    <CBtn @click="doBatchDel()">批量删除</CBtn>
                </div>
                <CPageTable :data="tableData" :curPage="paging.curPage" :total="paging.total" 
                    @changeSelect="changeSelect"
                    @changeSize="changeSize"
                    @changePage="search">
                    <template slot-scope="scope">
                        <CBtn type="text" @click="showVote(scope.row)">授权</CBtn>
                        <CBtn type="text" @click="showEdit(scope.row)">修改</CBtn>
                        <CBtn type="text" @click="doDel(scope.row)">删除</CBtn>
                    </template>

                </CPageTable>
            </div>
        </div>

        <CDailog title="新增/编辑" :isShow="isShowEdit" @close="isShowEdit = false" @enter="doSave">
            <el-form :inline="true">
                <CText form v-model="mm.code" title="编码" field="code"></CText>
                <CText form v-model="mm.name" title="名称" field="name"></CText>
            </el-form>
        </CDailog>
        <CDailog title="角色授权" :isShow="isShowVote" @close="isShowVote = false" @enter="doVote">
            <div style="width: 100%;">
                <el-tree ref="menuTree" :data="treeData" show-checkbox node-key="id" :default-expanded-keys="[]" :default-checked-keys="checkedIds"
                    :props="treeProps">
                </el-tree>
            </div>
        </CDailog>
    </div>
</template>

<script>
import pagingMixin from '@/mixin/pagingMixin'
export default {
    mixins: [pagingMixin],
    data() {
        return {
            treeData: [],
            treeProps: {
                children: 'listChild',
                label: 'label'
            },
            model: "ums",
            table: "UmsRole",
            isShowVote: false,
            curRoleId: "",
            checkedIds: []
        }
    },
    created() {
        this.templateId = "a2c515cf-0b4f-435c-8d87-77ec185c1ed8";

        //取得查询条件
        this.$sys.getPageConditions({ id: this.templateId }).then(res => {
            this.listCondition = res.data;
        })
        //查询表格数据
        this.search(1);
    },
    methods: {
        showVote(row) {
            this.$ums.getAllVoteMenu({id: row.id}).then(res => {
                this.treeData = res.data.tree;
                this.checkedIds = res.data.ownIds;
                this.isShowVote = true;
                this.curRoleId = row.id;
            })
            
        },
        doVote() {
            let checkedNodes = this.$refs.menuTree.getCheckedNodes(false, true);

            let ids = checkedNodes.map(item => {
                console.log(item);
                return item.id;
            });
            this.$ums.saveVote({
                curRoleId: this.curRoleId,
                listPermissionIds: ids
            }).then(res => {
                this.$u.showResultMsg(res);
                this.isShowVote = false;
            })
        }
    }
}
</script>

<style lang="scss" scoped></style>